package handler

import (
	"gozero-microservices/gateway-api/internal/middlewares"
	"gozero-microservices/gateway-api/internal/svc"
	"net/http"

	// "gozero_single_framework/tools"

	// sentinelPlugin "github.com/alibaba/sentinel-golang/pkg/adapters/go-zero"

	// "github.com/zeromicro/go-zero/core/jsonx"
	"github.com/zeromicro/go-zero/rest"
)

/*
 +----------------------------------------------------------------------
 + Title        : 路由
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2019-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 路由理器
 +----------------------------------------------------------------------
*/

// 定义路由结构体
type Routes struct {
	Server      *rest.Server
	ServerCtx   *svc.ServiceContext
	basePath    string
	Middlewares []rest.Middleware
}

/**
 * 分组
 *
 * @param string relativePath ----------------------必传,相对路径
 * @return *Routes
 * @author huwl
 */
func (r *Routes) Group(relativePath string) *Routes {
	return &Routes{
		Server:      r.Server,
		ServerCtx:   r.ServerCtx,
		basePath:    relativePath,
		Middlewares: r.Middlewares,
	}
}

/**
 * 处理器
 *
 * @param string method ----------------------------必传,方法
 * @param string path ------------------------------必传,路径
 * @param http.HandlerFunc handler -----------------必传,处理函数
 * @author huwl
 */
func (r *Routes) Handler(method, path string, handler http.HandlerFunc) {
	r.Server.AddRoutes(
		rest.WithMiddlewares(
			// 路由中间件
			r.Middlewares,
			[]rest.Route{
				{
					Method:  method,
					Path:    r.basePath + path,
					Handler: handler,
				},
			}...,
		),
	)
}

/**
 * 注册处理器
 *
 * @param *rest.Server server ----------------------必传,服务
 * @param *svc.ServiceContext serverCtx ------------必传,服务上下文
 * @author huwl
 */
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
	r := &Routes{
		Server:    server,
		ServerCtx: serverCtx,
		Middlewares: []rest.Middleware{
			// 全局中间件函数方式
			middlewares.CoresMiddleware,
			middlewares.ParseArgsMiddleware,
			middlewares.LimiterMiddleware,
		},
	}

	// --------------- (认证)前端分组路由开始 ---------------
	frontendAuthRoute := r.Group("/api/frontend/v1")
	// 设置认证中间件
	frontendAuthRoute.Middlewares = append(frontendAuthRoute.Middlewares, (&middlewares.FrontendAuthMiddleware{}).AuthUser)

	// --------------- 用户路由开始 ---------------
	// frontendAuthRoute.Handler(http.MethodGet, "/user/detail", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Detail)
	// frontendAuthRoute.Handler(http.MethodGet, "/user/list", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).List)
	// frontendAuthRoute.Handler(http.MethodPost, "/user/edit", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Edit)
	// --------------- 用户路由结束 ---------------
	// --------------- (认证)前端分组路由结束 ---------------

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// +++++++++++++++++++++++++++ (非认证)前端分组路由开始 +++++++++++++++++++++++++++
	frontendUnauthRoute := r.Group("/api/frontend/v1")

	// --------------- 首页路由开始 ---------------
	// 首页
	frontendUnauthRoute.Handler(http.MethodGet, "/", (&IndexHandler{BaseHandler{SvcCtx: serverCtx}}).Index)

	// 案例
	frontendUnauthRoute.Handler(http.MethodGet, "/index/demo", (&IndexHandler{BaseHandler{SvcCtx: serverCtx}}).Demo)

	// mongo案例
	frontendUnauthRoute.Handler(http.MethodGet, "/index/mongoDemo", (&IndexHandler{BaseHandler{SvcCtx: serverCtx}}).MongoDemo)

	// --------------- 首页路由结束 ---------------

	// --------------- 用户路由开始 ---------------
	// 注册
	frontendUnauthRoute.Handler(http.MethodPost, "/user/register", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Register)

	// 用户名登录
	frontendUnauthRoute.Handler(http.MethodPost, "/user/login", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Login)

	// 列表
	frontendUnauthRoute.Handler(http.MethodGet, "/user/list", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).List)

	// 数量
	// frontendUnauthRoute.Handler(http.MethodGet, "/user/total", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Total)

	// 登录
	// frontendUnauthRoute.Handler(http.MethodPost, "/user/login", (&UserHandler{BaseHandler{SvcCtx: serverCtx}}).Login)
	// --------------- 用户路由结束 ---------------

	// --------------- 区域路由开始 ---------------
	// 详情
	frontendUnauthRoute.Handler(http.MethodGet, "/region/detail", (&RegionHandler{BaseHandler{SvcCtx: serverCtx}}).Detail)

	// 列表
	frontendUnauthRoute.Handler(http.MethodGet, "/region/list", (&RegionHandler{BaseHandler{SvcCtx: serverCtx}}).List)
	// --------------- 区域路由结束 ---------------

	// --------------- 导航路由开始 ---------------
	// 列表
	frontendUnauthRoute.Handler(http.MethodGet, "/nav/list", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).List)
	// --------------- 导航路由结束 ---------------

	// ------------- 收货地址路由开始 -------------
	// 详情
	frontendUnauthRoute.Handler(http.MethodGet, "/userDeliveryAddr/detail", (&UserDeliveryAddrHandler{BaseHandler{SvcCtx: serverCtx}}).Detail)
	// ------------- 收货地址路由结束 -------------

	// --------------- 订单路由开始 ---------------
	// 下单
	frontendUnauthRoute.Handler(http.MethodPost, "/order/generateOrder", (&OrderHandler{BaseHandler{SvcCtx: serverCtx}}).GenerateOrder)

	// 列表
	frontendUnauthRoute.Handler(http.MethodGet, "/order/list", (&OrderHandler{BaseHandler{SvcCtx: serverCtx}}).List)
	// --------------- 订单路由结束 ---------------
	// +++++++++++++++++++++++++++ (非认证)前端分组路由结束 +++++++++++++++++++++++++++

	// +++++++++++++++++++++++++++ (非认证)管理分组路由结束 +++++++++++++++++++++++++++
	mgrUnauthRoute := r.Group("/api/mgr/v1")
	// --------------- 横幅路由开始 ---------------
	// 新增
	mgrUnauthRoute.Handler(http.MethodPost, "/banner/add", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).Add)

	// 删除
	mgrUnauthRoute.Handler(http.MethodGet, "/banner/delete", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).Delete)

	// 编辑
	mgrUnauthRoute.Handler(http.MethodPost, "/banner/edit", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).Edit)

	// 编辑状态
	mgrUnauthRoute.Handler(http.MethodGet, "/banner/editStatus", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).EditStatus)

	// 详情
	mgrUnauthRoute.Handler(http.MethodGet, "/banner/detail", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).Detail)

	// 列表
	mgrUnauthRoute.Handler(http.MethodGet, "/banner/list", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).List)

	// 数量
	mgrUnauthRoute.Handler(http.MethodGet, "/banner/total", (&BannerHandler{BaseHandler{SvcCtx: serverCtx}}).Total)
	// --------------- 横幅路由结束 ---------------

	// --------------- 导航路由开始 ---------------
	// 新增
	mgrUnauthRoute.Handler(http.MethodPost, "/nav/add", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).Add)

	// 删除
	mgrUnauthRoute.Handler(http.MethodGet, "/nav/delete", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).Delete)

	// 编辑
	mgrUnauthRoute.Handler(http.MethodPost, "/nav/edit", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).Edit)

	// 编辑状态
	mgrUnauthRoute.Handler(http.MethodGet, "/nav/editStatus", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).EditStatus)

	// 详情
	mgrUnauthRoute.Handler(http.MethodGet, "/nav/detail", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).Detail)

	// 列表
	mgrUnauthRoute.Handler(http.MethodGet, "/nav/list", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).List)

	// 数量
	mgrUnauthRoute.Handler(http.MethodGet, "/nav/total", (&NavHandler{BaseHandler{SvcCtx: serverCtx}}).Total)
	// --------------- 导航路由结束 ---------------
	// +++++++++++++++++++++++++++ (非认证)管理分组路由结束 +++++++++++++++++++++++++++
}
